package com.形为型模式.状态模式;



/**
 * 抽象状态类
 */
abstract  class ThreadState{
    protected String stateName;
}

/**
 * 环境类
 */
class ThreadContext{
    private ThreadState state;
    ThreadContext(){
        state = new New();
    }
    void start(){
        ((New) state).start(this);
    }
    void getCPU(){
        ((Runnable)state).getCPU(this);
    }
    void suspend(){
        ((Running)state).suspend(this);
    }
    void stop(){
        ((Running)state).stop(this);
    }
    void resume(){
        ((Blocked)state).resume(this);
    }

    public ThreadState getState() {
        return state;
    }

    public void setState(ThreadState state) {
        this.state = state;
    }
}

/**
 * 新建状态
 */
class New extends ThreadState{
    New(){
        stateName = "新建状态";
        System.out.println(String.format("当前状态处于：%s",stateName));
    }
    void start(ThreadContext hj){
        System.out.println("调用start()方法");
        if(stateName.equals("新建状态")){
            hj.setState(new Runnable());
        }
        else{
            System.out.println("当前状态不是新建状态，不能调用 START()方法");
        }
    }
}
/**
 *死亡状态
 */
class Dead extends ThreadState{
    Dead(){
        stateName =  "死亡状态";
        System.out.println(String.format("当前线程处于：%s",stateName));
    }
}

/**
 * 就绪状态
 */
class Runnable extends ThreadState{
    Runnable(){
        stateName="就绪状态";
        System.out.println(String.format("当前状态处于：%s",stateName));
    }
    void getCPU(ThreadContext hj){
        System.out.println("获取cpu时间 ");
        if(stateName.equals(
                "就绪状态")){
            hj.setState(new Running());
        }
        else{
            System.out.println("当前状态不是就绪状态，不能获取cpu");
        }
    }

}

/**
 * 阻塞状态
 */
class Blocked extends  ThreadState{
    Blocked(){
        stateName = "阻塞状态";
        System.out.println(String.format("当前线程处于:%s",stateName));
    }
    void resume(ThreadContext hj){
        System.out.println("调用resume方法");
        if(stateName.equals("阻塞状态")){
            hj.setState(new Runnable());
        }
        else{
            System.out.println("当前线程不是阻塞状态，不能调用 resume方法");
        }
    }
}
/**
 * 运行状态
 */
class Running extends ThreadState{
    public Running()
    {
        stateName="运行状态";
        System.out.println("当前线程处于：运行状态.");
    }
    public void suspend(ThreadContext hj)
    {
        System.out.print("调用suspend()方法-->");
        if(stateName.equals("运行状态"))
        {
            hj.setState(new Blocked());
        }
        else
        {
            System.out.println("当前线程不是运行状态，不能调用suspend()方法.");
        }
    }
    public void stop(ThreadContext hj)
    {
        System.out.print("调用stop()方法-->");
        if(stateName.equals("运行状态"))
        {
            hj.setState(new Dead());
        }
        else
        {
            System.out.println("当前线程不是运行状态，不能调用stop()方法.");
        }
    }
}

/**
 * 多线程的状态模式
 */
public class StateThread {
    public static void main(String[] args) {
        ThreadContext context = new ThreadContext();
        context.start();
        context.getCPU();
        context.suspend();
        context.resume();
        context.getCPU();
        context.stop();


    }
}
